<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\Linkslists;
use App\Models\Memberorders;
use App\Services\CacheService;
use DB,Log;

class Linkslimitpv extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'links:limitpv';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '每天0点更新链接最大访问量,剩余量';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        DB::table("links_lists")->select("id","created_at","users_id")->where("disable",1)->where("limit_pv",">",10000)->orderBy('id')->chunk(100, function ($links_lists) {
            $day = date("d",time());
            foreach ($links_lists as $links_list) {
                if (date("d",$links_list->created_at) == $day) {
                    // 获取用户会员
                    $order = Memberorders::ordersDetial($links_list->users_id);
                    if (empty($order)) {
                        $limit_pv = 100;
                        $left_pv = 100;
                        $limit_status = 1;
                        $end_time = 0;
                    } else {
                        $product_id = $order->product_id;
                        if ($product_id == 2) {
                            $limit_pv = 50000;
                            $left_pv = 50000;
                            $limit_status = 1;
                        } elseif ($product_id == 3) {
                            $limit_pv = 500000;
                            $left_pv = 500000;
                            $limit_status = 1;
                        } elseif ($product_id == 4) {
                            $limit_pv = 1000000;
                            $left_pv = 1000000;
                            $limit_status = 2;
                        } else {
                            $limit_pv = 100;
                            $left_pv = 100;
                            $limit_status = 1;
                        }
                        $end_time = $order->end_time;
                    }
                    Linkslists::updateLinksInfo($links_list->id,["limit_pv"=>$limit_pv,"left_pv"=>$left_pv,"limit_status"=>$limit_status,"end_time"=>$end_time]);
                }
            }
        });
    }
}
